const api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cookbookType: 1,
    cookbookId: '',
    lunch: {},
    dinner: {}
  },

  loadLunch() {
    this.setData({
      lunch: {}
    })
    api.get('/cart?cookbookType=1').then(res => {
      this.setData({
        lunch: res.data,
        cookbookId: res.data.cookbookId
      })
    })
  },

  loadDinner() {
    this.setData({
      dinner: {}
    })
    api.get('/cart?cookbookType=2').then(res => {
      this.setData({
        dinner: res.data,
        cookbookId: res.data.cookbookId
      })
    })
  },

  onChangeTab (e) {
    this.setData({
      cookbookType: parseInt(e.detail.name)
    })
    this.loadCart()
  },

  loadCart () {
    switch (this.data.cookbookType) {
      case 1: 
      this.loadLunch()
      break
      case 2: 
      this.loadDinner()
      break
    }
  },

  selectFood (e) {
    const cart = e.currentTarget.dataset.cart
    const food = e.currentTarget.dataset.food
    const cartUpdateBo = {
      selected: e.detail
    }
    api.put(`/cart?foodId=${food.id}&cookbookId=${cart.cookbookId}&cookbookType=${this.data.cookbookType}`, cartUpdateBo).then(res => {
      if(res.code === 200) {
        if (this.data.cookbookType === 1) {
          this.setData({
            lunch: res.data
          })
        } else {
          this.setData({
            dinner: res.data
          })
        }
      }
    })
  },

  selectAllFood (e) {
    let param = ''
    if(e.detail) {
      param = 'selectAll'
    } else {
      param = 'unSelectAll'
    }
    api.post(`/cart/${param}?cookbookId=${this.data.cookbookId}&cookbookType=${this.data.cookbookType}`).then(res => {
      if(res.code === 200) {
        if (this.data.cookbookType === 1) {
          this.setData({
            lunch: res.data
          })
        } else {
          this.setData({
            dinner: res.data
          })
        }
      }
    })
  },
  changeQuantity (e) {
    const cart = e.currentTarget.dataset.cart
    const food = e.currentTarget.dataset.food
    const cartUpdateBo = {
      quantity: e.detail
    }
    api.put(`/cart?foodId=${food.id}&cookbookId=${cart.cookbookId}&cookbookType=${this.data.cookbookType}`, cartUpdateBo).then(res => {
      if(res.code === 200) {
        if (this.data.cookbookType === 1) {
          this.setData({
            lunch: res.data
          })
        } else {
          this.setData({
            dinner: res.data
          })
        }
      }
    })
  },

  deleteCart (e) {
    const cart = e.currentTarget.dataset.cart
    const food = e.currentTarget.dataset.food
    api.del(`/cart?foodId=${food.id}&cookbookId=${cart.cookbookId}&cookbookType=${this.data.cookbookType}`).then(res => {
      if(res.code === 200) {
        if (this.data.cookbookType === 1) {
          this.setData({
            lunch: res.data
          })
        } else {
          this.setData({
            dinner: res.data
          })
        }
      }
    })

  },

  onSubmit() {
    wx.navigateTo({
      url: `/pages/settle/settle?cookbookType=${this.data.cookbookType}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadCart()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadCart()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})